package model

import (
	"chat_srv/basic/config"
	"time"
)

// 群聊表
type ImGroup struct {
	Id             uint      `gorm:"column:id;type:bigint UNSIGNED;comment:主键id;primaryKey;not null;" json:"id"`                  // 主键id
	GroupId        string    `gorm:"column:group_id;type:varchar(64);comment:群组id;not null;" json:"group_id"`                     // 群组id
	ConversationId string    `gorm:"column:conversation_id;type:varchar(64);comment:对应会话id;not null;" json:"conversation_id"`     // 对应会话id
	GroupName      string    `gorm:"column:group_name;type:varchar(128);comment:群名称;not null;" json:"group_name"`                 // 群名称
	GroupAvatar    string    `gorm:"column:group_avatar;type:varchar(255);comment:群头像;default:NULL;" json:"group_avatar"`         // 群头像
	Announcement   string    `gorm:"column:announcement;type:text;comment:群公告;" json:"announcement"`                              // 群公告
	OwnerId        uint      `gorm:"column:owner_id;type:bigint UNSIGNED;comment:群主id;not null;" json:"owner_id"`                 // 群主id
	MemberCount    uint      `gorm:"column:member_count;type:int UNSIGNED;comment:群人员数;default:NULL;" json:"member_count"`        // 群人员数
	MaxMembers     uint      `gorm:"column:max_members;type:int UNSIGNED;comment:最大成员数;not null;default:500;" json:"max_members"` // 最大成员数
	Status         uint      `gorm:"column:status;type:bigint UNSIGNED;comment:状态:1-正常 2-解散;not null;default:1;" json:"status"`   // 状态:1-正常 2-解散
	CreatedAt      time.Time `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt      time.Time `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
}

// 设置与数据库映射
func (ig *ImGroup) TableName() string {
	return "im_group"
}

// 添加群聊
func (ig *ImGroup) Create() error {
	return config.DB.Create(&ig).Error
}

// 查询群聊
func (ig *ImGroup) GetImGroup(where *ImGroup) error {
	return config.DB.Where(where).Limit(1).Find(&ig).Error
}

// 获取可加入的聊天室
func (ig *ImGroup) GetPublicGroups(userId uint64) (groups []*ImGroup, err error) {
	err = config.DB.
		Table("im_group").
		Where("member_count < max_members AND NOT EXISTS (SELECT 1 FROM im_group_member WHERE group_id = im_group.group_id AND user_id = ?)", userId).
		Find(&groups).Error
	return groups, err
}

// 获取好友加入的群组
func (ig *ImGroup) GetFriendJoinedGroups(userId uint64) (groups []*ImGroup, err error) {
	err = config.DB.
		Table("im_group_member").
		Select("im_group.*").
		Joins("JOIN im_group ON im_group_member.group_id = im_group.group_id").
		Where("im_group_member.user_id = ? AND im_group.is_public = 1", userId).
		Find(&groups).Error
	return groups, err
}

// 获取群成员数量
func (ig *ImGroup) GetGroupMemberCount(groupId string) (count int64, err error) {
	err = config.DB.
		Table("im_group_member").
		Where("group_id = ?", groupId).
		Count(&count).Error
	return count, err
}

// 获取热门群组，按照群成员数量排序
func (ig *ImGroup) GetHotGroups(limit int) (groups []*ImGroup, err error) {
	err = config.DB.
		Table("im_group").
		Order("member_count DESC").
		Limit(limit).
		Find(&groups).Error
	return groups, err
}

// 获取用户在群聊中的好友数量
func (ig *ImGroup) GetUserFriendCountInGroup(userId uint64, groupId string) (count int64, err error) {
	err = config.DB.
		Table("im_group_member").
		Where("group_id = ? AND user_id IN (SELECT friend_id FROM im_friend WHERE user_id = ?)", groupId, userId).
		Count(&count).Error
	return count, err
}

// 获取用户已加入的群组
func (ig *ImGroup) GetUserJoinedGroups(userId uint64) (groups []*ImGroup, err error) {
	err = config.DB.
		Table("im_group_member").
		Select("im_group.*").
		Joins("JOIN im_group ON im_group_member.group_id = im_group.group_id").
		Where("im_group_member.user_id = ?", userId).
		Find(&groups).Error
	return groups, err
}
